package com.zhihuichongcheng.application.ui.im.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.exceptions.HyphenateException;
import com.smartlib.cmnObject.util.DialogUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.ui.im.adapter.GroupAdapter;

import java.util.List;

/**
 * Created by 3water on 2016/10/30.
 */

public class ImGroupListActivity extends BaseActivity {
    private RecyclerView mRcvGroupList;
    private GroupAdapter mAdapter = null;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);
        initView();
        initData();
    }

    private void initView()
    {
        setTitle("我的群组");
        mRcvGroupList = (RecyclerView)findViewById(R.id.rcv_group_list);
        mAdapter = new GroupAdapter(mContext);
        mRcvGroupList.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvGroupList.setAdapter(mAdapter);

    }

    private void initData()
    {

        DialogUtil.showLoadDiadlog(mContext);

new Thread(new Runnable() {
    @Override
    public void run() {
        //从服务器获取自己加入的和创建的群组列表，此api获取的群组sdk会自动保存到内存和db。
        try {

            final List<EMGroup> grouplist = EMClient.getInstance().groupManager().getJoinedGroupsFromServer();//需异步处理
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    DialogUtil.dismiss(mContext);
                mAdapter.setData(grouplist);
                    mAdapter.notifyDataSetChanged();
                }
            });
        } catch (HyphenateException e) {
            e.printStackTrace();
        }

    }
}).start();
    }



}
